<?php

namespace App\Http\Controllers\home;

use App\Model\weblog;
use App\Model\writer;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class weBlogController extends Controller
{
    //
    public function blog(Request $request, $id)
    {
          if($request->isMethod('post')){
               $writer = writer::where('id',$id)->get();
               foreach($writer as $k){
                   $nickname = $k->nickname;
               }
               $Blog = new weblog();
              $rules=array(
                  'text'=>'required',
              );
              $mess=array(
                  'text.required'=>'内容不可为空',
              );
              $this->validate($request,$rules,$mess);
               $Blog->wid = $id;
               $Blog->text = $request->text;
               $Blog->addtime = time();
               $Blog->save();
              return redirect('huweize/home/weblog/'.$id);
          }
        $sum=DB::table('writers')->rightjoin('weblogs','.weblogs.wid','writers.id')->where('writers.id',session('writerid'))->paginate(2);
        
        return view('huweize.home.weblog',compact('sum'));

    }

    public function DEL(Request $request,$id)
    {
        $new=weblog::find($id);
        if (count($new)){
            $de = weblog::find($id);
            $de->delete();
            return back();
//            return redirect('huweize/home/weblog/del/');
        }else{
            return back();
//            return redirect('huweize/home/weblog/del/');

        }


    }
}
